/**
 * @desc : 自定义列表预览
 * @author : xianggang
 * @date : 2019-09-01
 */
var app = angular.module ( 'formCustSqlViewApp' , [ 'baseDirective' , "pasvaz.bindonce" ] );
app.controller ( "formCustSqlViewController" , [ '$scope' , 'baseService' , 'formCustSqlViewService' , 'ArrayToolService' , function ( $scope , baseService , formCustSqlViewService , ArrayTool ) {

	var code = $.getParam ( "code" );
	$scope.vo = {
		code : code ,
	}

	// 初始化数据
	$scope.init = function ( ) {

		// 获取自定义查询对象
		$.ajax ( {
	        url : getCtxUrl ( '/form/formCustSql/view/vo_' + code , true ),
	        data : {},
	        type : 'POST' ,
	        contentType : 'application/json' ,
	        dataType : 'json' ,
	        async : false ,
	        success : function ( data ) {
				if ( !data.isOk ) {
					$.Dialog.error ( data.msg );
					return;
				}
				
				$scope.vo = data.data;
				
				$scope.vo.isInit = true;
				
				// 字段列表
				$scope.fields = data.data.fieldsList;
				
				// 查询列表
				$scope.conditions = JsonUtil.ps ( data.data.conditions ) || [];
				// 按钮列表
				$scope.buttonList = JsonUtil.ps ( data.data.buttons ) || [];
				// 扩展参数
				$scope.expand = JsonUtil.ps ( data.data.expand ) || {};

				// 数据查询地址
				$scope.vo.listUrl = __ctx + "/form/formCustSql/view/list_" + code;
				// sql调试地址
				$scope.vo.sqlUrl = __ctx + "/form/formCustSql/view/sql_" + code;
				// 数据导出地址
				$scope.vo.exportUrl = __ctx + "/form/formCustSql/view/export_" + code;
				// 数据导入地址
				$scope.vo.importUrl = __ctx + "/form/formCustSql/view/import_" + code;
				
				// 按钮页面
				$scope.searchBtnToolsHtml = '../../../form/formCustSql/view/search-tool-page.html';
				
				// 按钮权限编码
				$scope.vo.exportOperAlias = "customsql_export_" + code;
				
				// 页面参数
				$scope.pageParams = {};
				var params = $.getParam ( "initParam" );
				params = JsonUtil.ps(params) || {};
				params = $.extend(params,$.getParams());
				// 删掉自定义列表的code参数
				delete params.code;
				if ( params ) {
					if ( typeof param == "string" ) {
						$scope.pageParams.params = params;
					} else {
						$scope.pageParams.params = JSON.stringify ( params );
					}
				}
				// 初始化参数
				$scope.vo.initParam = $.extend ( {} , true , params );
				window.initParam = $scope.vo.initParam ;

				formCustSqlViewService.init ( {
				    scope : $scope ,
				    vo : $scope.vo
				} );
				
				$scope.$broadcast("sqlVoInited", {});
				
				!$scope.$$phase&&$scope.$digest();
			
	        }
		 } );
		
	}

	$scope.init ( );
} ] )

// service层
.service ( 'formCustSqlViewService' , [ 'baseService' , function ( baseService ) {

	var service = {
	    init : function ( options ) {

		    var $scope = options.scope;
		    var vo = options.vo;

		    // 在父窗口设置一个全局的Scope
		    window.parent[vo.code + "Scope"] = $scope;
		    $scope.window = window;

		    this.initExpand ( $scope );
		    this.initFields ( $scope );
		    this.initCondition ( $scope );

	    } ,
	    // 扩展参数,方便后期升级
	    initExpand : function($scope){
			$scope.expand = $scope.vo.expand || {};
	    },
	    // 处理字段列表
	    initFields : function ( $scope ) {

		    var fields = $scope.fields;
		    for ( var i = 0 ; i < fields.length ; i++ ) {

			    var field = fields[i];

			    var ctrl = JsonUtil.ps ( field.controlContent ) || {};
			    // 扩展信息
			    field.controlContent = ctrl;

			    // 如果是下拉框,单复选框 , 则需要格式化一下
			    if ( [ 4 , 6 , 8 ].indexOf ( field.controlType ) >= 0 ) {
				    field.dateFormat = JsonUtil.ps ( field.dateFormat );
			    }

			    // 如果支持自定义格式化,则需要用bindHtml来渲染
			    if ( field.controlType == 19 || field.unit ) {
				    field.bindHtml = true;
			    }

			    // 是否有跳转
			    if ( field.url ) {
				    field.hasDetailJump = true;
			    }

			    // 校验规则
			    if ( field.rules ) {
				    field.rules = JsonUtil.ps ( field.rules );
			    }
		    }
	    } ,
	    // 初始化查询
	    initCondition : function ( $scope ) {

		    var conditions = $scope.conditions;

		    if ( conditions ) {
			    var initParam = $scope.vo.initParam;
			    for ( var i = 0 ; i < conditions.length ; i++ ) {
				    var c = conditions[i];
				    if ( c.json ) {
					    if ( [ 4 , 6 , 8 ].indexOf ( c.controlType ) >= 0 ) {
						    c.json = JsonUtil.ps ( c.json );
					    }
				    }
				    c.v = c.val;
				    if ( initParam && initParam.hasOwnProperty ( c.name ) ) {
					    c.v = initParam[c.name];
				    }
				    delete c.val;
			    }
		    }

	    } ,
	    bindParent : function ( scope , name , pName , callback ) {

		    // 给各个模块监听并且绑定对象的属性..
	    	function initParentInfo(){
			    var scopeName = name + "Scope";
			    var idName = "__" + name + "Id";
			    scope.parent[scopeName] = scope;
			    scope[scopeName] = scope;
			    scope.sqlVoInited = true;
			    var alias = "";
			    if ( scope.parent.vo ) {
				    alias = scope.parent.vo.alias;
			    }
			    scope[idName] = ( pName ? ( pName + "_" ) : "" ) + name + "_" + alias + "_" + GetRandomNum ( 1 , 100000 );
			    callback && callback.call ( this );
	    	}
	    	
	    	if(scope.parent.vo.isInit){
	    		initParentInfo();
	    	}else{
		    	scope.$on ( "sqlVoInited" , function ( event , msg ) {
		    		initParentInfo();
		    	});
	    	}

	    }
	}
	return service;

} ] )
